﻿using System;
using UnityEngine;

// Token: 0x02000003 RID: 3
public class AI : MonoBehaviour
{
	// Token: 0x06000004 RID: 4 RVA: 0x00002098 File Offset: 0x00000298
	private void Start()
	{
		this.voice = base.GetComponent<VoiceScript>();
	}

	// Token: 0x06000005 RID: 5 RVA: 0x000020A8 File Offset: 0x000002A8
	public void Update()
	{
		this.timeSinceSpawn += Time.deltaTime;
		this.checkTime -= Time.deltaTime;
		if (this.checkTime < 0f)
		{
			this.checkTime = 0.3f;
			if (!this.target)
			{
				foreach (Collider2D collider2D in Physics2D.OverlapCircleAll(this.bodyScript.head.transform.position, this.visionSize))
				{
					WeaponScript weaponScript;
					if (this.bodyScript.canPickup && !this.bodyScript.holdingMain && collider2D.TryGetComponent<WeaponScript>(out weaponScript) && ((this.bodyScript.lookDir > 0f && weaponScript.transform.position.x > this.bodyScript.torso.transform.position.x) || (this.bodyScript.lookDir < 0f && weaponScript.transform.position.x < this.bodyScript.torso.transform.position.x) || Vector2.Distance(weaponScript.transform.position, this.bodyScript.head.transform.position) < 4.5f) && !weaponScript.held)
					{
						this.targetWeapon = weaponScript;
					}
					DamageReceiver damageReceiver;
					if (collider2D.TryGetComponent<DamageReceiver>(out damageReceiver) && damageReceiver.body.alive && damageReceiver.body.team != this.bodyScript.team && ((this.bodyScript.lookDir > 0f && damageReceiver.transform.position.x > this.bodyScript.torso.transform.position.x) || (this.bodyScript.lookDir < 0f && damageReceiver.transform.position.x < this.bodyScript.torso.transform.position.x) || Vector2.Distance(damageReceiver.transform.position, this.bodyScript.head.transform.position) < 6f))
					{
						this.target = damageReceiver.body;
						if (this.voice)
						{
							this.voice.Alert();
						}
					}
				}
			}
			else if (this.target)
			{
				RaycastHit2D[] array2 = Physics2D.LinecastAll(this.bodyScript.head.position, this.target.head.position);
				int i = 0;
				while (i < array2.Length)
				{
					RaycastHit2D raycastHit2D = array2[i];
					if (!this.bodyScript.limbCols.Contains(raycastHit2D.collider) && !this.target.limbCols.Contains(raycastHit2D.collider) && !raycastHit2D.collider.GetComponent<DamagingObject>())
					{
						this.blockedSight = true;
						this.blockedPos = raycastHit2D.point.x;
						DamageReceiver damageReceiver2;
						if (raycastHit2D.collider.TryGetComponent<DamageReceiver>(out damageReceiver2) && damageReceiver2.body.alive && damageReceiver2.body.team != this.bodyScript.team)
						{
							this.blockedSight = false;
							this.target = damageReceiver2.body;
							break;
						}
						break;
					}
					else
					{
						this.blockedSight = false;
						i++;
					}
				}
				if (!this.blockedSight && this.bodyScript.holdingMain && Vector2.Distance(this.bodyScript.torso.transform.position, this.target.torso.transform.position) > 15f && Random.Range(0f, 1f) < this.throwChance && !this.bodyScript.heldFirearm)
				{
					this.bodyScript.ThrowWeapon();
				}
			}
			if (!this.currentlyAttacking)
			{
				this.currentlyAttacking = (Random.Range(0f, 1f) < this.aggresion);
			}
			else
			{
				this.currentlyAttacking = (Random.Range(0f, 1f) > this.aggresion);
			}
			if (this.target)
			{
				if ((this.attackTime < 0f && this.target.isAttacking && Vector2.Distance(this.target.head.position, this.bodyScript.head.transform.position) < 5.5f && Random.Range(0f, 1f) < this.reflexes && !this.bodyScript.heldFirearm) || (this.target.heldFirearm && Vector2.Distance(this.target.head.position, this.bodyScript.head.transform.position) > 5.5f))
				{
					this.bodyScript.isBlocking = true;
					if (!this.target.heldFirearm && Random.Range(0f, 1f) < this.dodgeChance)
					{
						this.bodyScript.Dodge();
					}
				}
				else
				{
					this.bodyScript.isBlocking = false;
				}
			}
			if (!this.bodyScript.holdingMain)
			{
				this.bodyScript.Pickup(null);
			}
			this.curAiBox = Physics2D.OverlapCircle(this.bodyScript.torso.position, 2f, LayerMask.GetMask(new string[]
			{
				"AIBox"
			}));
		}
		this.attackTime -= Time.deltaTime;
		this.attackCooldown -= Time.deltaTime;
	}

	// Token: 0x06000006 RID: 6 RVA: 0x000026CC File Offset: 0x000008CC
	private void FixedUpdate()
	{
		if (!this.bodyScript.holdingMain && this.targetWeapon)
		{
			if (!this.curAiBox)
			{
				if (this.bodyScript.torso.transform.position.x < this.targetWeapon.transform.position.x)
				{
					this.bodyScript.moveDir.x = 1f;
				}
				this.bodyScript.moveDir.x = -1f;
			}
			else if (this.bodyScript.torso.transform.position.x > this.curAiBox.transform.position.x)
			{
				this.bodyScript.moveDir.x = 1f;
			}
			else
			{
				this.bodyScript.moveDir.x = -1f;
			}
			this.bodyScript.lookingPos = this.targetWeapon.transform.position;
			if (Vector2.Distance(this.bodyScript.torso.transform.position, this.targetWeapon.transform.position) < 3f)
			{
				this.bodyScript.Pickup(null);
			}
			if (this.targetWeapon.held)
			{
				this.targetWeapon = null;
				return;
			}
		}
		else if (this.target)
		{
			float num = this.desiredAttackDistance;
			if (!this.currentlyAttacking)
			{
				num += this.desiredFallbackDistance;
			}
			if (this.blockedSight)
			{
				num += 0.5f;
			}
			if (this.bodyScript.torso.transform.position.x < this.target.head.transform.position.x)
			{
				num *= -1f;
			}
			if (this.blockedSight)
			{
				num += this.blockedPos;
			}
			else
			{
				num += this.target.torso.transform.position.x;
			}
			if (!this.curAiBox)
			{
				if (this.bodyScript.torso.transform.position.x > num)
				{
					this.bodyScript.moveDir.x = -1f;
				}
				else
				{
					this.bodyScript.moveDir.x = 1f;
				}
			}
			else if (this.bodyScript.torso.transform.position.x > this.curAiBox.transform.position.x)
			{
				this.bodyScript.moveDir.x = 1f;
			}
			else
			{
				this.bodyScript.moveDir.x = -1f;
			}
			this.bodyScript.lookingPos = this.target.head.transform.position;
			if (Mathf.Abs(this.target.torso.position.x - this.bodyScript.torso.position.x) < this.shoveDistance && !this.bodyScript.heldFirearm)
			{
				this.bodyScript.Shove();
			}
			if (Vector2.Distance(this.target.head.position, this.bodyScript.head.transform.position) < this.attackStartDistance && this.attackCooldown < 0f && !this.blockedSight)
			{
				if (!this.bodyScript.holdingMain)
				{
					this.attackCooldown = this.bodyScript.unarmedDesiredAttackTime / this.bodyScript.attackTimeMult + this.desiredAttackCooldown + Random.Range(-0.3f, 0.3f);
					this.attackTime = this.bodyScript.unarmedDesiredAttackTime / this.bodyScript.attackTimeMult + Random.Range(-0.2f, 0.2f);
				}
				else
				{
					this.attackCooldown = this.bodyScript.held.desiredAttackTime / this.bodyScript.attackTimeMult + this.desiredAttackCooldown + Random.Range(-0.3f, 0.3f);
					this.attackTime = this.bodyScript.held.desiredAttackTime / this.bodyScript.attackTimeMult + Random.Range(-0.2f, 0.2f);
				}
			}
			if (this.attackTime > 0f)
			{
				this.bodyScript.Attack();
				if (this.bodyScript.heldFirearm)
				{
					this.attackTime = 0f;
					this.attackCooldown = this.bodyScript.held.desiredAttackTime + Random.Range(-0.7f, 0.7f);
				}
			}
			else
			{
				this.bodyScript.StopAttack();
			}
			if (this.timeSinceSpawn > 5f && this.doJump && this.target.torso.position.y - this.bodyScript.torso.position.y > 1.8f)
			{
				this.bodyScript.Jump();
			}
			if (!this.target.alive)
			{
				this.target = null;
				if (this.voice)
				{
					this.voice.Cheer();
					return;
				}
			}
		}
		else
		{
			this.bodyScript.lookingPos = this.bodyScript.torso.transform.position + Vector3.right * 10f * this.bodyScript.lookDir;
			this.bodyScript.moveDir = Vector2.zero;
			this.bodyScript.StopAttack();
		}
	}

	// Token: 0x04000002 RID: 2
	public BodyScript bodyScript;

	// Token: 0x04000003 RID: 3
	public BodyScript target;

	// Token: 0x04000004 RID: 4
	public WeaponScript targetWeapon;

	// Token: 0x04000005 RID: 5
	[Tooltip("How far away the grunt will check when watching for enemies")]
	public float visionSize;

	// Token: 0x04000006 RID: 6
	private float checkTime;

	// Token: 0x04000007 RID: 7
	[Range(0f, 1f)]
	public float aggresion;

	// Token: 0x04000008 RID: 8
	[Range(0f, 1f)]
	public float reflexes;

	// Token: 0x04000009 RID: 9
	[Range(0f, 1f)]
	public float throwChance;

	// Token: 0x0400000A RID: 10
	[Range(0f, 1f)]
	public float dodgeChance;

	// Token: 0x0400000B RID: 11
	private bool currentlyAttacking;

	// Token: 0x0400000C RID: 12
	private float attackTime;

	// Token: 0x0400000D RID: 13
	private float attackCooldown;

	// Token: 0x0400000E RID: 14
	public float desiredAttackCooldown;

	// Token: 0x0400000F RID: 15
	public float desiredAttackDistance;

	// Token: 0x04000010 RID: 16
	public float desiredFallbackDistance;

	// Token: 0x04000011 RID: 17
	[SerializeField]
	private bool blockedSight;

	// Token: 0x04000012 RID: 18
	private float blockedPos;

	// Token: 0x04000013 RID: 19
	public float shoveDistance;

	// Token: 0x04000014 RID: 20
	public float attackStartDistance = 5.5f;

	// Token: 0x04000015 RID: 21
	private VoiceScript voice;

	// Token: 0x04000016 RID: 22
	[Tooltip("If true, the entity will jump if their target is above/below them.")]
	public bool doJump;

	// Token: 0x04000017 RID: 23
	private float timeSinceSpawn;

	// Token: 0x04000018 RID: 24
	private Collider2D curAiBox;
}
